package com.ruoyi.web.controller.baodao.controller;


import com.ruoyi.baodao.entity.*;
import com.ruoyi.baodao.mapper.BDMapper;
import com.ruoyi.baodao.mapper.MFGoods9999Mapper;
import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.web.controller.baodao.service.GoodsService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * 商品主治编辑 页面
 */
@Slf4j
@Controller
@RequestMapping("baodao/goodszz")
public class GoodsZZController extends BaseController {
    @Autowired
    MFGoods9999Mapper mfGoods9999Mapper;
    @Autowired
    BDMapper bdMapper;
    @Autowired
    GoodsService goodsService;

    private String prefix = "baodao/goodszz";

    @GetMapping()
    public String operlog()
    {
        return prefix + "/goodszz";
    }

    // 查询 商品主档
    /**
     * 药品主档
     */
    @PostMapping("/list")
    @ResponseBody
    @DataSource(value = DataSourceType.SLAVE)
    public TableDataInfo list(MFGoods9999 mfGoods9999, String flag)
    {
        // 获取 用户名 和  用户 部门
        SysUser sysUser = (SysUser) SecurityUtils.getSubject().getPrincipal();
        // 取部门编号（门店号）
        sysUser.getDept();
        String shopId = sysUser.getDept().getDeptName();
        log.info("登录用户信息" +sysUser.toString());
        log.info("登录用户：" + sysUser.getLoginName() + "所属门店" + sysUser.getDept().getDeptName() );
        log.info("查询药品主档,标识参数flag ：{}",flag);
        logger.info("查询药品主档,参数:{}",mfGoods9999.getPlucode());
        logger.info("查询药品主档,参数:{}",mfGoods9999.getClassname());
        logger.info("查询药品主档,价格:{}",mfGoods9999.getPrice());
        // List<MFGoodsLink> mfGoodsLinks = mfGoodsLinkMapper.selectByExample(new MFGoodsLinkExample());
        startPage(); //分页

/*        MFGoods9999Example mfGoods9999Example = new MFGoods9999Example();
        MFGoods9999Example.Criteria criteria = mfGoods9999Example.createCriteria();
        if(mfGoods9999.getPlucode() != null && !mfGoods9999.getPlucode().equals("")){
            criteria.andPlucodeEqualTo(mfGoods9999.getPlucode());}
        if(mfGoods9999.getGoodscode() != null && !mfGoods9999.getGoodscode().equals("")){
            criteria.andGoodscodeEqualTo(mfGoods9999.getGoodscode());
        }
        if(mfGoods9999.getGoodsname() != null && !mfGoods9999.getGoodsname().equals("")){
            criteria.andGoodsnameLike("%" + mfGoods9999.getGoodsname() + "%");
        }
        if(mfGoods9999.getClassname() != null && !mfGoods9999.getClassname().equals("")){
            criteria.andClassnameEqualTo(mfGoods9999.getClassname());
        }*/

        // 新增限制 条件
//        List<String> strings = new ArrayList<>();
//
//        if(mfGoodsLinks != null && mfGoodsLinks.size() != 0)
//        {
//            for (MFGoodsLink m:
//                    mfGoodsLinks ) {
//                strings.add(m.getGoodscode());
//            }
//        }
//
//        criteria.andGoodscodeNotIn(strings);

        // List<MFGoods9999> mfGoods9999s =   mfGoods9999Mapper.selectByExample(mfGoods9999Example);
        log.info("是否去除已存入关联表数据（flag3 为1 去除）: " +  mfGoods9999.getFlag3());
        BigDecimal bigDecimal = mfGoods9999.getPrice();
        String price = "";
        if(bigDecimal != null){
            price = bigDecimal.toString();
        }
        // 20210205
       // List<MFGoods9999> mfGoods9999s =   bdMapper.queryGoods(mfGoods9999.getGoodscode(),mfGoods9999.getGoodsname(),shopId,mfGoods9999.getClassname(),mfGoods9999.getFlag3(),price);
        List<MFGoods9999> mfGoods9999s =   bdMapper.queryGoods(mfGoods9999.getGoodscode(),mfGoods9999.getGoodsname(),shopId,mfGoods9999.getClassname(),null,price);
        // 去除所有维护表中数据

        logger.info(mfGoods9999s.toString());
        //  List<MFGoods9999> mfGoods9999s1 = companyService.selectCompanyAll(company);
        return getDataTable(mfGoods9999s);

    }


    // 新增 和编辑 商品主治
    @PostMapping("/updateGoodsZZ")
    @ResponseBody
    @DataSource(value = DataSourceType.SLAVE)
    public AjaxResult updateGoodsZZ(@Param("goodsCode") String goodsCode, @Param("goodsZZ") String goodsZZ){
        logger.info("更新主治信息" + goodsCode + "   " + goodsZZ);
        // 获取 用户名 和  用户 部门
        SysUser sysUser = (SysUser) SecurityUtils.getSubject().getPrincipal();
        // 取部门编号（门店号）
        sysUser.getDept();
        String shopId = sysUser.getDept().getDeptName();

        long result =  bdMapper.updateGoodsZZ(goodsCode,goodsZZ,sysUser.getDept().getDeptName());
        if(result >= 1){
            return success();
        }else
            return error();
    }

    // 新增 和 编辑价格
    @PostMapping("/updatePrice")
    @ResponseBody
    @DataSource(value = DataSourceType.SLAVE)
    public AjaxResult updatePrice(@Param("goodsCode") String goodsCode, @Param("price") String price){
        logger.info("更新商品价格信息" + goodsCode + "   " + price);
        // 获取 用户名 和  用户 部门
        SysUser sysUser = (SysUser) SecurityUtils.getSubject().getPrincipal();
        // 取部门编号（门店号）
        sysUser.getDept();
        String shopId = sysUser.getDept().getDeptName();
        if(price.equals("") || price == null)
            price = "0.00";

        long result=   bdMapper.updatePrice(goodsCode,new BigDecimal(price),sysUser.getDept().getDeptName());
        if(result >= 1){
            return success();
        }else
            return error();
    }


    @PostMapping("/export")
    @ResponseBody
    @DataSource(value = DataSourceType.SLAVE)
    public AjaxResult export(MFGoods9999 mfGoods9999)

    {
        // 获取 用户名 和  用户 部门
        SysUser sysUser = (SysUser) SecurityUtils.getSubject().getPrincipal();
        // 取部门编号（门店号）
        sysUser.getDept();
        String shopId = sysUser.getDept().getDeptName();
        BigDecimal bigDecimal = mfGoods9999.getPrice();
        String price = "";
        if(bigDecimal != null){
            price = bigDecimal.toString();
        }
        List<MFGoods9999> mfGoods9999s =   bdMapper.queryGoods(mfGoods9999.getGoodscode(),mfGoods9999.getGoodsname(),shopId,mfGoods9999.getClassname(),mfGoods9999.getFlag3(),price);
        ExcelUtil<MFGoods9999> util = new ExcelUtil<MFGoods9999>(MFGoods9999.class);
        return util.exportExcel(mfGoods9999s, "商品主治信息");
    }

    @GetMapping("/importTemplate")
    @ResponseBody
    public AjaxResult importTemplate()
    {
        ExcelUtil<MFGoods9999> util = new ExcelUtil<MFGoods9999>(MFGoods9999.class);
        return util.importTemplateExcel("商品主治信息");
    }

    @PostMapping("/importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
/*        ExcelUtil<MFGoods9999> util = new ExcelUtil<MFGoods9999>(MFGoods9999.class);
        List<MFGoods9999> mfGoods9999s = util.importExcel(file.getInputStream());
        System.out.println(mfGoods9999s);
        String operName = ShiroUtils.getSysUser().getLoginName();
        logger.info("导入数据" + mfGoods9999s.get(0).getGoodscode());*/

        ExcelUtil<ImportData1> util = new ExcelUtil<ImportData1>(ImportData1.class);
        List<ImportData1> mfGoods9999s = util.importExcel(file.getInputStream());
        System.out.println(mfGoods9999s);
        String operName = ShiroUtils.getSysUser().getLoginName();
       // logger.info("导入数据" + mfGoods9999s.get(0).getGoodscode());
        logger.info("开始导入数据: " + "是否支持覆盖:" + updateSupport);
        String message = goodsService.importGoodsZZ(mfGoods9999s, updateSupport, operName);
        logger.info("数据导入结束");

        return AjaxResult.success(message);
    }

}
